//uno
module.exports.createRoom = function(roomId,channelService,GameRemote) {
	console.log("创建房间111:"+roomId)
	var gameBasic = require("./basic/gameBasic.js")
	var playerNum = 6
	var room = new gameBasic(roomId,"UNO",6,channelService,GameRemote)
	room.min_player = 3
	room.roomMode = "UNO"
	room.luckys = new Array(playerNum)
	for(var i = 0;i < room.luckys.length;i++){
		room.luckys[i] = 0
	}
	room.maxGameNumber = 1
	room.gameNumber = 0
	var logicBasic = require("./logic/UNOLogic.js")
	var logic = new logicBasic()
	var local = {}
	local.curPlayer = -1 			//当前出牌玩家
	local.lastPlayer = -1 			//最后出牌玩家
	local.lastCard = false 			//最后出的牌
	room.playTimer = 0 				//出牌定时器
	local.PLAY_TIME = 15000 		//出牌时间
	local.END_PLAY_TIME = 0         //出牌结束时间
	local.direction = 1 			//当前方向
	local.curNum = false 			//当前数字
	local.curCol = false 			//当前颜色
	local.curType = false   		//当前牌型
	local.overlay = 0 				//+2，+4叠加数量
	local.councilScores = {} 		//本局分数列表
	room.gameBegin = function() {
		console.log("gameBegin")
		//重置本局分数
		local.councilScores = {}
		logic.shuffle()
		var playerList = []
		room.forEachReady(function(player) {
			playerList.push(player.chair)
		})
		logic.sendCard(playerList)
		local.curPlayer = playerList[Math.floor(Math.random() * playerList.length)]
		room.forEachReady(function(player) {
			var notify = {
				cmd : "gameBegin",
				handCard : logic.playerHandler[player.chair].slice(0),
				chair : player.chair,
				curPlayer : local.curPlayer,
				dice : {value : local.curPlayer,col: Math.floor(Math.random() * 4),type : "num"}
			}
			room.sendByUid(player.uid,notify)
		})
		local.END_PLAY_TIME = (new Date()).getTime() + local.PLAY_TIME
		room.playTimer = setTimeout(local.notPlayCard,local.PLAY_TIME)
	}
	//下一个玩家出牌
	local.nextPlay = function() {
		clearTimeout(room.playTimer)
		for(var i = 0;i < playerNum;i++){
			local.curPlayer = (local.curPlayer + local.direction + playerNum) % playerNum
			if(room.players[local.curPlayer].isActive){
				break
			}
		}
		var notify = {
			cmd : "nextPlay",
			curPlayer : local.curPlayer,
			curCol : local.curCol,
			curNum : local.curNum,
			curType : local.curType,
			lastCard : local.lastCard,
			overlay : local.overlay
		}
		room.sendAll(notify)
		local.END_PLAY_TIME = (new Date()).getTime() + local.PLAY_TIME
		room.playTimer = setTimeout(local.notPlayCard,local.PLAY_TIME)
	}
	//跳过下一个玩家
	local.skipPlayer = function(){
		for(var i = 0;i < playerNum;i++){
			local.curPlayer = (local.curPlayer + local.direction + playerNum) % playerNum
			if(room.players[local.curPlayer].isActive){
				break
			}
		}
		var notify = {
			cmd : "skip",
			chair : local.curPlayer
		}
		room.sendAll(notify)
	}
	//抽牌
	local.drawCard = function(chair,count) {
		var tmpCards = logic.drawCard(chair,count)
		room.forEachReady(function(player) {
			var notify = {
				cmd : "drawCard",
				count : tmpCards.length,
				chair : chair
			}
			if(player.chair == chair){
				notify.cards = tmpCards
				notify.handCard = logic.playerHandler[chair].slice(0)
			}
			room.sendByUid(player.uid,notify)
		})
	}
	//检查是否能打  颜色相同或数字相同、或万能牌
	local.checkCard = function(index,col) {
		var card = logic.playerHandler[local.curPlayer][index]
		if(logic.isCanPlay(card,col,local.curNum,local.curCol,local.curType,logic.playerHandler[local.curPlayer].length)){
			return true
		}else{
			return false
		}
	}
	//完成出牌
	local.finishPlayCard = function(index,col) {
		var card = logic.playCard(local.curPlayer,index)
		local.lastCard = card
		local.lastPlayer = local.curPlayer
		// console.log(" card : ",card," index : ",index,"col : ".col)
		switch(card.type){
			case "num":
				local.curNum = card.value
				local.curCol = card.col
				local.curType = false
			break
			case "draw":
				local.curNum = false
				local.curCol = card.col
				local.curType = card.type
				local.overlay += 2
			break
			case "skip":
				local.curNum = false
				local.curCol = card.col
				local.curType = card.type
			break
			case "reverse":
				local.curNum = false
				local.curCol = card.col
				local.direction = -local.direction
				local.curType = card.type
			break
			case "wild":
				local.curNum = false
				local.curCol = col
				if(card.value === 5){
					local.curType = "wild"
					local.overlay += 4
				}else{
					local.curType = false
				}
			break
		}
		// console.log("local.curNum : ",local.curNum,"  local.curCol : ",local.curCol)
		var notify = {
			cmd : "playCard",
			card : card,
			chair : local.curPlayer,
			index : index,
			curNum : local.curNum,
			curCol : local.curCol,
			curType : local.curType
		}
		room.sendAll(notify)
		//判断胜利
		if(logic.playerHandler[local.curPlayer].length == 0){
			local.settlement()
			return
		}
		// switch(card.type){
		// 	case "num":
		// 	break
		// 	case "draw":
		// 		local.skipPlayer()
		// 		local.drawCard(local.curPlayer,2)
		// 	break
		// 	case "skip":
		// 		local.skipPlayer()
		// 	break
		// 	case "reverse":
		// 	break
		// 	case "wild":
		// 		if(card.value == 5){
		// 			local.notQuery()
		// 			return
		// 		}
		// 	break
		// }
		switch(card.type){
			case "skip":
				local.skipPlayer()
			break
		}
		local.nextPlay()
	}
	//未出牌处理
	local.notPlayCard = function() {
		if(local.lastPlayer != local.curPlayer){
			if(local.overlay){
				local.drawCard(local.curPlayer,local.overlay)
				local.overlay = 0
				local.curType = false
			}else{
				local.drawCard(local.curPlayer,1)
			}
			local.nextPlay()
		}else{
			local.finishPlayCard(0,0)
		}
	}
	//分数改变
	local.changeScore = function(chair,score) {
		if(!local.scores[chair]){
			local.scores[chair] = 0
		}
		local.scores[chair] += score
		if(!local.councilScores[chair]){
			local.councilScores[chair] = 0
		}
		local.councilScores[chair] += score
		var notify = {
			cmd : "changeScore",
			chair : chair,
			changeScore : score,
			score : local.scores[chair],
			council : local.councilScores[chair]
		}
		room.sendAll(notify)
	}
	//结束
	local.settlement = function() {
		clearTimeout(room.playTimer)
		var allScore = 0
		for(var i = 0;i < room.players.length;i++){
			if(i !== local.curPlayer && room.players[i].isReady){
				var tmpScore = 0
				for(var j = 0;j < logic.playerHandler[i].length;j++){
					var card = logic.playerHandler[i][j]
					switch(card.type){
						case "num":
							tmpScore += card.value
						break
						case "draw":
							tmpScore += 20
						break
						case "skip":
							tmpScore += 20
						break
						case "reverse":
							tmpScore += 20
						break
						case "wild":
							tmpScore += 50
						break
					}
				}
				allScore += tmpScore
				local.changeScore(i,-tmpScore)
			}
		}
		console.log("allScore : ",allScore)
		local.changeScore(local.curPlayer,allScore)
		var notify = {
			cmd : "settlement",
			win : local.curPlayer,
			playerHandler : logic.playerHandler,
			councils : local.councilScores
		}
		room.sendAll(notify)
		room.settlement()
		room.gameNumber++
		if(room.gameNumber >= room.maxGameNumber){
			room.gameOverBefore(local.curPlayer,local.scores)
		}
	}
	//获取当前房间信息
	room.handle.getRoomInfo = function(uid,sid,param,cb) {
		var chair = room.chairMap[uid]
		var handRest = {}
		for(var i in logic.playerHandler){
			handRest[i] = logic.playerHandler[i].length
		}
		var info = {
			roomType : room.roomType,
			roomMode : room.roomMode,
			roomId : room.roomId,
			state : room.beginFlag,
			PLAY_TIME : local.PLAY_TIME,
			END_PLAY_TIME : local.END_PLAY_TIME,
			players : room.getPlayersInfo(),
			countDownEndTime : room.countDownEndTime,
			curTime : (new Date()).getTime(),
			curPlayer : local.curPlayer,
			handCard : logic.playerHandler[chair],
			chair : chair,
			handRest : handRest,
			direction : local.direction,
			curNum : local.curNum,
			curCol : local.curCol,
			curType : local.curType,
			lastCard : local.lastCard,
			overlay : local.overlay,
			type : room.matchType,
			willWin : room.willWin,
			mustPlay : room.mustPlay,
			graded : room.graded,
			rate : room.rate,
			charge : room.charge,
			maxGameNumber : room.maxGameNumber,
			gameNumber : room.gameNumber
		}
		cb(true,info)
	}
	//放弃出牌
	room.handle.giveUp = function(uid,sid,param,cb) {
		var chair = room.chairMap[uid]
		if(chair !== local.curPlayer){
			cb(false,"还没到你的轮次")
			return
		}
		local.notPlayCard()
		cb(true)
	}
	//出牌
	room.handle.playCard = function(uid,sid,param,cb) {
		var chair = room.chairMap[uid]
		if(chair !== local.curPlayer){
			cb(false,"还没到你的轮次")
			return
		}
		// test1
		// if(room.players[chair].uid > 10000){
		// 	local.settlement()
		// 	cb(true)
		// 	return
		// }
		var index = param.index
		var col = param.col
		// console.log("实际打的牌 : ", param.index,logic.playerHandler[chair],logic.playerHandler[chair][index])
		if(typeof(index) !== "number" || index < 0 || !logic.playerHandler[chair][index]){
			cb(false,"出牌参数错误")
			return
		}
		if(local.checkCard(index,col)){
			local.finishPlayCard(index,col)
			cb(true)
		}else{
			console.log("playCard false")
			cb(false)
		}
	}
	room.getLocal = function() {
		return local
	}
	room.getLogic = function() {
		return logic
	}
	return room
}